package com.jhgsys.internal.sypq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;

import com.jhgsys.internal.common.exception.FebsException;

import com.jhgsys.internal.sypq.entity.Notice;
import com.jhgsys.internal.sypq.service.INoticeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.validation.Valid;
import java.util.Map;

import java.util.Date;

/**
 * 待办通知表 Controller
 *
 * @author ganjun
 * @date 2024-11-30 21:43:28
 */
@Slf4j
@Validated
@Controller
public class NoticeController extends BaseController {

    @Autowired
    private INoticeService noticeService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "notice")
    @RequiresPermissions("notice:view")
    public String noticeIndex(){
        return jumpPage("sypq/notice/noticeList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "notice/add")
    @RequiresPermissions("notice:add")
    public String noticeAdd(){
        return jumpPage("sypq/notice/noticeEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "notice/update/{id}")
    @RequiresPermissions("notice:edit")
    public String noticeUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("notice", noticeService.findNoticeById(id));
        return jumpPage("sypq/notice/noticeEdit");
    }

    /**
     * 查询（所有）
     * @author ganjun
     * @date 2024-11-30 21:43:28
     */
    @GetMapping("notice")
    @ResponseBody
    @RequiresPermissions("notice:view")
    public FebsResponse getAllNotices(Notice notice) {
        try {
            return success(noticeService.findNoticeList(notice));
        }catch (Exception ex){
            return failure(ex);
        }

    }

    /**
     * 查询（分页）
     * @author ganjun
     * @date 2024-11-30 21:43:28
     */
    @GetMapping("notice/list")
    @ResponseBody
    @RequiresPermissions("notice:view")
    public FebsResponse noticeList(QueryRequest request, Notice notice) {
        try {
            Map<String, Object> dataTable = getDataTable(this.noticeService.findNoticePage(request, notice));
            return success(dataTable);
        }catch (Exception ex){
            return failure(ex);
        }
    }

    /**
     * 新增 Notice
     * @author ganjun
     * @date 2024-11-30 21:43:28
     */
    @Log("新增Notice")
    @PostMapping("notice")
    @ResponseBody
    @RequiresPermissions("notice:add")
    public FebsResponse addNotice(@Valid Notice notice) throws FebsException {
        try {
            this.noticeService.createNotice(notice);
            return success();
        } catch (Exception ex) {
            return failure(ex,"新增失败");
        }
    }

    /**
     * 逻辑删除 Notice
     * @author ganjun
     * @date 2024-11-30 21:43:28
     */
    @Log("逻辑删除Notice")
    @GetMapping("notice/delete/{id}")
    @ResponseBody
    @RequiresPermissions("notice:delete")
    public FebsResponse deleteNotice(@PathVariable("id") String noticeIds) throws FebsException {
        try {
            Notice notice = new Notice();
            notice.setDeletedBy(getCurrentUser().getUserName());
            notice.setDeletedTime(new Date());
            notice.setDeletedFlg(FebsConstant.INVALID);
            this.noticeService.deleteNotice(notice,noticeIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
    * 物理删除 Notice
    * @author ganjun
    * @date 2024-11-30 21:43:28
    */
    @Log("物理删除Notice")
    @GetMapping("notice/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("notice:delete")
    public FebsResponse physicallyDeleteNotice(@PathVariable("id") String noticeIds) throws FebsException {
        try {
            this.noticeService.physicallyDeleteNotice(noticeIds);
            return success();
        } catch (Exception ex) {
            return failure(ex,"删除失败");
        }
    }

    /**
     * 修改 Notice
     * @author ganjun
     * @date 2024-11-30 21:43:28
     */
    @Log("修改Notice")
    @PostMapping("notice/update")
    @ResponseBody
    @RequiresPermissions("notice:edit")
    public FebsResponse updateNotice(Notice notice) throws FebsException {
        try {
            this.noticeService.updateNotice(notice);
            return success();
        } catch (Exception ex) {
            return failure(ex,"修改失败");
        }
    }

    @Log("首页待办")
    @GetMapping("notice/todoList")
    @ResponseBody
    public FebsResponse todoList(Notice notice) {
        try {
            return success(noticeService.todoList(notice));
        }catch (Exception ex){
            return failure(ex);
        }
    }

    @Log("首页待办已读")
    @GetMapping("notice/done/{id}")
    @ResponseBody
    public FebsResponse done(@PathVariable("id") String noticeId) {
        try {
            noticeService.done(noticeId);
            return success();
        }catch (Exception ex){
            return failure(ex);
        }
    }

}
